package com.huike.contract.service.impl;

import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbRulePool;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ISysUserService;
import com.huike.clues.service.ITbRulePoolService;
import com.huike.clues.utils.HuiKeCrmDateUtils;
import com.huike.clues.utils.JobUtils;
import com.huike.common.constant.Constants;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.contract.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TransferServiceImpl implements ITransferService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ITbRulePoolService rulePoolService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TbClueMapper clueMapper;


    @Autowired
    private TbBusinessMapper businessMapper;


    @Override
    public List<TransferVo> selectTransferList(SysUser user) {
        List<SysUser> sysUsers = sysUserMapper.selectUserList(user);
        List<TransferVo> list = new ArrayList<>();
        for (SysUser sysUser : sysUsers) {
            TransferVo transferVo = new TransferVo();
            //clueNum    businessNum
            List<TbClue> tbClues = clueMapper.countAllAssignClueByUser(sysUser);
            List<TbBusiness> tbBusinesses = businessMapper.countAllAssignBusinessByUser(sysUser);
            BeanUtils.copyProperties(sysUser,transferVo);
            transferVo.setClueNum(tbClues.size());
            transferVo.setBusinessNum(tbBusinesses.size());
            list.add(transferVo);
        }

        return list;
    }
    /*
     * 转派线索或者商机
    * */

    @Override
    public void transferByUserId(String type, Long userId, Long transferUserId) {

            //查出原有用户所持有信息   BY  userID
        List<TbAssignRecord> tbAssignRecords = assignRecordMapper.selectAssignRecordByUserId(userId, type);
        //将查出信息  userId  换为新对象的id  transferUserId
        for (TbAssignRecord tbAssignRecord : tbAssignRecords) {
            tbAssignRecord.setUserId(transferUserId);
            tbAssignRecord.setLatest("1");
            //原有用户的此条信息  by   userId
            assignRecordMapper.updateAssignRecord(tbAssignRecord);
        }


    }
}
